# # 获取用户输入的字符串，例如 "1,-2,6,11,-5"
# lst = input()
# # 将输入字符串按逗号分割成字符串列表
# lst = lst.split(',')
# # 将字符串列表转换为整数列表
# lst = [int(x) for x in lst]

# # 定义递归函数，计算列表中所有整数绝对值之和
# def abs_sum(L):
#     # 基本情况：如果列表为空，返回0
#     if L == []:
#         return 0
#     # 递归步骤：第一个元素的绝对值加上剩余部分的绝对值和
#     return abs(L[0]) + abs_sum(L[1:])

# # 打印绝对值之和
# print(abs_sum(lst))



import jieba
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# Step 1: Load file
with open("三国演义.txt", "r", encoding="utf-8") as f:
    text = f.read()

# Step 2: Word segmentation
words = jieba.lcut(text)

# Step 3: Count target names
target_names = ['孔明', '刘备', '曹操', '关羽', '周瑜', '张飞', '吕布', '赵云', '孙权', '司马懿']
counts = Counter()

for word in words:
    if word in target_names:
        counts[word] += 1

# Step 4: Output top 10
top10 = counts.most_common(10)
print("Top 10 Characters:")
for name, count in top10:
    print(f"{name}: {count} 次")

# Step 5: Word cloud
wc_text = ' '.join([name for name, _ in top10 for _ in range(counts[name])])

wc = WordCloud(font_path="/usr/share/fonts/truetype/arphic/ukai.ttc", width=800, height=600, background_color="white")
wc.generate(wc_text)
wc.to_file("wordcloud.png")

# Optional: Show it
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
